package com.contactstranslate.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.contactstranslate.R;
import com.contactstranslate.component.CTEventBroadcastReceiver;
import com.contactstranslate.model.CTContactModel;
import com.contactstranslate.model.CTNumberModel;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class CTUtils {

	public static final String ARG_SECTION_NUMBER = "section_number";
	public static final String ARG_CREDIT = "section_number";
	public static final String ARG_PHONE_CONTACT_COUNT = "ARG_PHONE_CONTACT_COUNT";
	public static final String ARG_SIM_CONTACT_COUNT = "ARG_SIM_CONTACT_COUNT";
	public static final String ARG_SELECTION_TAB = "ARG_SELECTION_TAB";
	public static int UNITE = 300;
	public static int REMISE = 50;
	public static String DEFAULT_CURRENCY_CODE = "EUR";
	public static double DEFAULT_CURRENCY_AMOUNT = 655.97;
	public static final String SHARE_PREF_KEY_EMAIL = "rodriguetaponno@gmail.com";
	public static String NOTIFICATION_ID = "notification_id";

	public static int calculCout(int nombreContact, Context c){

		Log.e("Utils", nombreContact+"");
		if(nombreContact > 500  && nombreContact <= 1000)
			return Integer.parseInt(c.getResources().getStringArray(R.array.ct_product_cost)[1]);
		if(nombreContact > 1000  && nombreContact <= 1500)
			return Integer.parseInt(c.getResources().getStringArray(R.array.ct_product_cost)[2]);
		if(nombreContact > 1500  && nombreContact <= 2000)
			return Integer.parseInt(c.getResources().getStringArray(R.array.ct_product_cost)[3]);
		if(nombreContact > 2000  && nombreContact <= 2500)
			return Integer.parseInt(c.getResources().getStringArray(R.array.ct_product_cost)[4]);


		return Integer.parseInt(c.getResources().getStringArray(R.array.ct_product_cost)[0]);
	}

	public static void savePreference(String key, String value, Context context) {
		try {
			SharedPreferences sharedPref = PreferenceManager
					.getDefaultSharedPreferences(context);
			SharedPreferences.Editor editor = sharedPref.edit();
			editor.putString(key, value);
			editor.commit();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getPreference(String key, Context context) {
		try {
			SharedPreferences sharedPref = PreferenceManager
					.getDefaultSharedPreferences(context);
			String value = sharedPref.getString(key, "");
			return value;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}

	}

	public static boolean containt(String myString, String exp) {
		CharSequence cs = exp;
		boolean result = myString.contains(cs);
		return result;
	}

	public static List<String> splitToList(String myString, String exp) {
		List<String> result = new ArrayList<String>();
		StringTokenizer st = new StringTokenizer(myString, exp);
		int i = 0;
		while (st.hasMoreElements()) {
			result.add(st.nextToken());
		}
		return result;
	}

	public static int getContactCount(Activity context,List<CTContactModel> listOffContact) {

		int i = 0;
		for (CTContactModel ctContactModel : listOffContact) {
			if (ctContactModel.isSelected() && (ctContactModel.getConvertCode()==Integer.valueOf(context.getString(R.string.to_converted_int)).intValue()||ctContactModel.getConvertCode()==Integer.valueOf(context.getString(R.string.notall_converted_int)).intValue()))
				for (CTNumberModel ctNumber : ctContactModel.getNumbers()) {
					if (ctNumber.isCameroonian() && ctNumber.isConvertable()) {
						i++;
					}
				}
		}
		return i;
	}

	public static int getConvertContactCount(Activity context,List<CTContactModel> listOffContact) {

		int i = 0;
		for (CTContactModel ctContactModel : listOffContact) {
			if (ctContactModel.isSelected() && (ctContactModel.getConvertCode()==Integer.valueOf(context.getString(R.string.is_converted_int)).intValue()||ctContactModel.getConvertCode()==Integer.valueOf(context.getString(R.string.notall_converted_int)).intValue()))
				for (CTNumberModel ctNumber : ctContactModel.getNumbers()) {
					if (ctNumber.isCameroonian() && !ctNumber.isConvertable()) {
						i++;
					}
				}
		}
		return i;
	}

	public static void getProvider(Activity context, CTContactModel contactModel) {

		int mtn = R.drawable.mtn;
		int orange = R.drawable.orange;
		int camtel = R.drawable.camtel;
		int nexttel = R.drawable.nexttel;
		int special = R.drawable.special;
		boolean findToConvertable = false;
		boolean findConverted = false;
		boolean isConverted;

		contactModel.setConvertCode(Integer.valueOf(context
				.getString(R.string.notto_converted_int)));
		for (CTNumberModel ctNumber : contactModel.getNumbers()) {

			isConverted = false;
			String number = remSpace(ctNumber.getNumber());

			Pattern regexCameroonPatternConverted = Pattern.compile(context
					.getString(R.string.regex_cameroon_converted));
			Matcher regexCameroonMatcherConverted = regexCameroonPatternConverted
					.matcher(number);
			if (regexCameroonMatcherConverted.matches()) {
				String number1 = regexCameroonMatcherConverted
						.group(Integer.valueOf(context
								.getString(R.string.regex_cameroon_converted_bloc1)));
				String number2 = regexCameroonMatcherConverted
						.group(Integer.valueOf(context
								.getString(R.string.regex_cameroon_converted_bloc2)));
				if (number2 == null)
					continue;
				number = number2;
				if (number1 != null) {
					number = number1 + number;
				}
				isConverted = true;
				findConverted = true;
				ctNumber.setConvertable(false);
				ctNumber.setCameroonian(true);
				ctNumber.setRollBackNumber(number);
			}

			Pattern regexCameroonPattern = Pattern.compile(context
					.getString(R.string.regex_cameroon));
			Matcher regexCameroonMatcher = regexCameroonPattern.matcher(number);
			if (regexCameroonMatcher.matches()) {

				ctNumber.setCameroonian(true);

				if (number.matches(context
						.getString(R.string.regex_cameroon_mtn))) {

					if (isConverted) {
						ctNumber.setConverterNumber(ctNumber.getNumber());
						ctNumber.setProvider(mtn);
						continue;
					}

					Matcher regexCameroon = Pattern.compile(
							context.getString(R.string.regex_cameroon_mtn))
							.matcher(number);
					regexCameroon.find();
					String number1 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc1)));
					String number2 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc2)));

					if (number2 == null)
						continue;
					number = context.getString(R.string.regex_cameroon_mtn_add)
							+ number2;
					if (number1 != null) {
						number = number1 + number;
					}

					ctNumber.setConverterNumber(number);
					ctNumber.setProvider(mtn);
					ctNumber.setConvertable(true);

				} else if (number.matches(context
						.getString(R.string.regex_cameroon_orange))) {
					if (isConverted) {
						ctNumber.setConverterNumber(ctNumber.getNumber());
						ctNumber.setProvider(orange);
						continue;
					}

					Matcher regexCameroon = Pattern.compile(
							context.getString(R.string.regex_cameroon_orange))
							.matcher(number);
					regexCameroon.find();
					String number1 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc1)));
					String number2 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc2)));

					if (number2 == null)
						continue;
					number = context
							.getString(R.string.regex_cameroon_orange_add)
							+ number2;
					if (number1 != null) {
						number = number1 + number;
					}

					ctNumber.setConverterNumber(number);
					ctNumber.setProvider(orange);
					ctNumber.setConvertable(true);
				} else if (number.matches(context
						.getString(R.string.regex_cameroon_nexttel))) {

					if (isConverted) {
						ctNumber.setConverterNumber(ctNumber.getNumber());
						ctNumber.setProvider(nexttel);
						continue;
					}

					Matcher regexCameroon = Pattern.compile(
							context.getString(R.string.regex_cameroon_nexttel))
							.matcher(number);
					regexCameroon.find();
					String number1 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc1)));
					String number2 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc2)));

					if (number2 == null)
						continue;
					number = context
							.getString(R.string.regex_cameroon_nexttel_add)
							+ number2;
					if (number1 != null) {
						number = number1 + number;
					}

					ctNumber.setConverterNumber(number);
					ctNumber.setProvider(nexttel);
					ctNumber.setConvertable(true);
				} else if (number.matches(context
						.getString(R.string.regex_cameroon_camtel))) {
					if (isConverted) {
						ctNumber.setConverterNumber(ctNumber.getNumber());
						ctNumber.setProvider(camtel);
						continue;
					}

					Matcher regexCameroon = Pattern.compile(
							context.getString(R.string.regex_cameroon_camtel))
							.matcher(number);
					regexCameroon.find();
					String number1 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc1)));
					String number2 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc2)));

					if (number2 == null)
						continue;
					number = context
							.getString(R.string.regex_cameroon_camtel_add)
							+ number2;
					if (number1 != null) {
						number = number1 + number;
					}

					ctNumber.setConverterNumber(number);
					ctNumber.setProvider(camtel);
					ctNumber.setConvertable(true);
				} else if (number.matches(context
						.getString(R.string.regex_cameroon_special))) {
					if (isConverted) {
						ctNumber.setConverterNumber(ctNumber.getNumber());
						ctNumber.setProvider(special);
						continue;
					}

					Matcher regexCameroon = Pattern.compile(
							context.getString(R.string.regex_cameroon_special))
							.matcher(number);
					regexCameroon.find();
					String number1 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc1)));
					String number2 = regexCameroon.group(Integer
							.valueOf(context
									.getString(R.string.regex_cameroon_bloc2)));

					if (number2 == null)
						continue;
					number = context
							.getString(R.string.regex_cameroon_special_add)
							+ number2;
					if (number1 != null) {
						number = number1 + number;
					}

					ctNumber.setConverterNumber(number);
					ctNumber.setProvider(camtel);
					ctNumber.setConvertable(true);
				}
			}
			findToConvertable = ctNumber.isConvertable() ? ctNumber.isConvertable() : findToConvertable;

		}

		if (findToConvertable) {
			if (findConverted) {
				contactModel.setConvertCode(Integer.valueOf(context
						.getString(R.string.notall_converted_int)));
			} else {
				contactModel.setConvertCode(Integer.valueOf(context
						.getString(R.string.to_converted_int)));
			}
		} else if (findConverted) {
			contactModel.setConvertCode(Integer.valueOf(context
					.getString(R.string.is_converted_int)));
		}

		return;
	}

	public static String remSpace(String number) {
		number = number.trim();
		number = number.replaceAll("\\s+", "");
		number = number.replaceAll("-", "");
		return number;
	}

	public static int stringToInt(String str)
	{ 
		if(str==null|| "".equals(str.trim()))
		{
			return 0;
		}

		if(str.matches("-?\\d+"))
			return Integer.valueOf(str);
		else
			return 0;

	}

	public static boolean isInt(String str)
	{ 
		if(str==null|| "".equals(str.trim()))
		{
			return false;
		}
		return str.matches("-?\\d+");
	}

	/**
	 *  revoir l'instruction PendingIntent pendingIntent. pour la valeur de requestCode
	 * @param context
	 */
	public static void startAlarm(Context context){

		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 55);
		calendar.set(Calendar.SECOND, 0);

		int requestCode = new Random().nextInt(9999);

		Intent myIntent = new Intent(context, CTEventBroadcastReceiver.class);
		myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);		
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, requestCode, myIntent, 0);
		AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
		alarmManager.setRepeating(AlarmManager.RTC, calendar.getTimeInMillis(),2*AlarmManager.INTERVAL_DAY ,pendingIntent);

		savePreference(SHARE_PREF_EVENT_ALARM_ID, ""+requestCode, context);
	}

	public static void cancelAlarm(Context context){
		try{

			String alarmId = getPreference(SHARE_PREF_EVENT_ALARM_ID, context);
			Intent intent = new Intent(context, CTEventBroadcastReceiver.class);
			PendingIntent sender = PendingIntent.getBroadcast(context, Integer.parseInt(alarmId), intent, 0);
			AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
			alarmManager.cancel(sender);

		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static final String ACCOUNT_TYPE = "com.contactstranslate";
	public static final String PARAM_AUTHTOKEN_TYPE = "authToken";
	public static final String PARAM_USERNAME = "userid";
	public static final String PARAM_CONFIRMCREDENTIALS = "confirmCredentials";
	public static final String EVENT_NEXT_DATE="event_next_date";
	public static final String SHARE_PREF_EVENT_ALARM_ID = "com.contactconverter.EVENT_ALARM_ID";

	public static String [] getPRODUITS_MARCHANTS(Context ctx) { 
		return ctx.getResources().getStringArray(R.array.ct_product_id);
	}
	public static int com_contacttranslator_500 = 330;
	public static int com_contacttranslator_1000 = 660;
	public static int com_contacttranslator_1500 = 985;
	public static int com_contacttranslator_2000 = 1320;
	public static int com_contacttranslator_2500 = 1650;

	public static int getMajor(int nombreContact, Context c){
		
		if(nombreContact > 500  && nombreContact <= 1000)
			return 1000;
		if(nombreContact > 1000  && nombreContact <= 1500)
			return 1500;
		if(nombreContact > 1500  && nombreContact <= 2000)
			return 2000;
		if(nombreContact > 2000  && nombreContact <= 2500)
			return 2500;


		return 500;
	}

}
